<template src="./index.html"></template>

<script>
import MedicalRecordBaseInfoService from "../../../../../_services/medicalRecordBaseInfo.service";
export default {
  name: "TianDingPatientMeBasicInfoComponent",
  data() {
    return {
      user: {},
      BMI: 0,
      Age: 0,
      MateAge: 0,
      params: {
        BaseInfoID: 0,
        UserID: 0,
        Height: "140",
        Weight: "40",
        Max_BP: "80",
        Min_BP: "60",
        AllergyHistory: "",
        InfectionProject: []
      },
      showInfectionProject: false,
      infectionProjectValue: "",
      InfectionProjectTemp: [],
      infectionProjectList: [
        {
          key: -1,
          value: "未查待补",
          isRadio: true,
          check: false,
          show: true
        },
        {
          key: 0,
          value: "均为阴性",
          isRadio: true,
          check: false,
          show: true
        },
        {
          key: 1,
          value: "乙肝",
          isRadio: false,
          check: true,
          show: true
        },
        {
          key: 2,
          value: "丙肝",
          isRadio: false,
          check: false,
          show: true
        },
        {
          key: 3,
          value: "艾滋",
          isRadio: false,
          check: true,
          show: true
        },
        {
          key: 4,
          value: "梅毒",
          isRadio: false,
          check: false,
          show: true
        }
      ],
      showHeight: false,
      heightList: [],
      showWeight: false,
      weightList: [],
      showBP: false,
      Max_BPList: [],
      Min_BPList: [],
      load: false
    };
  },
  mounted() {
    this.user = JSON.parse(this.local$.getItem("tdUser"));

    this.utils$.getNumberLabel(this.heightList, 140, 200, "厘米");
    this.utils$.getNumberLabel(this.weightList, 40, 120, "公斤");
    this.utils$.getNumberLabel(this.Max_BPList, 80, 160, "mmHg");
    this.utils$.getNumberLabel(this.Min_BPList, 60, 100, "mmHg");
    this.getBaseInfo();
  },
  methods: {
    getBaseInfo() {
      this.indicator$.open("正在加载...");
      let params = {};
      params.UserID = this.user.UserID;
      MedicalRecordBaseInfoService.getBaseInfoByUserId(params).then(
        response => {
          if (response && response.Data) {
            this.params = response.Data;
            this.params.Height = this.params.Height.toString();
            this.params.Weight = this.params.Weight.toString();
            this.params.Max_BP = this.params.Max_BP.toString();
            this.params.Min_BP = this.params.Min_BP.toString();
            this.utils$.dataFormat(this.params);
          }

          let curr = this.utils$.getCurrentDate();
          this.Age = this.utils$.getAge(this.user.Birthday);
          this.MateAge = this.utils$.getAge(this.user.MateBirthDay);
          this.BMI = this.utils$.getBMI(this.params.Weight, this.params.Height);
          this.params.UserID = this.user.UserID;

          if (this.params.BaseInfoID > 0) {
            this.InfectionProjectTemp = this.params.InfectionProject;
            this.params.InfectionProject.filter((v, i, a) => {
              let item = this.infectionProjectList.find(
                i => i.key === v.ProjectName
              );
              v.Name = item.value;
              v.CheckTime = v.CheckTime.split("T")[0];
              return a;
            });
            this.infectionProjectList.filter((v, i, a) => {
              let include = this.params.InfectionProject.find(
                i => i.ProjectName === v.key
              );
              v.check = include === undefined ? false : true;
              return a;
            });
          }
          this.load = true;
          this.indicator$.close();
        }
      );
    },
    getHeight(event) {
      this.showHeight = false;
      this.params.Height = event;
      this.BMI = this.utils$.getBMI(
        parseInt(this.params.Weight),
        parseInt(event)
      );
    },
    getWeight(event) {
      this.showWeight = false;
      this.params.Weight = event;
      this.BMI = this.utils$.getBMI(
        parseInt(event),
        parseInt(this.params.Height)
      );
    },
    getMin_BP(event) {
      this.showBP = false;
    },
    changeInfectionProject() {
      if (this.showInfectionProject) {
        this.showInfectionProject = false;
      } else {
        this.showInfectionProject = true;
      }
    },
    getInfectionProject(event) {
      let curr = this.utils$.getCurrentDate();
      let temp = [];
      let list = [];
      if (event[0] === -1) {
        let item = this.infectionProjectList.find(i => i.key === event[0]);
        temp.push(item.value);
        this.infectionProjectValue = temp.toString();
        this.params.InfectionProject = [];
        this.showInfectionProject = false;
      } else if (event[0] === 0) {
        let item = this.infectionProjectList.find(i => i.key === event[0]);
        let nitem = this.InfectionProjectTemp.find(
          i => i.ProjectName === event[0]
        );

        let ele = {
          InfectionProjectID:
            nitem !== undefined ? nitem.InfectionProjectID : 0,
          Name: item.value,
          ProjectName: item.key,
          CheckHospital: nitem !== undefined ? nitem.CheckHospital : "",
          CheckTime:
            nitem !== undefined
              ? nitem.CheckTime.split("T")[0]
              : `${curr.year}-${curr.month}-${curr.day}`
        };
        list.push(ele);
        temp.push(item.value);
        this.infectionProjectValue = temp.toString();
        this.params.InfectionProject = list;
        this.showInfectionProject = false;
      } else {
        event.forEach(key => {
          let item = this.infectionProjectList.find(i => i.key === key);
          let nitem = this.InfectionProjectTemp.find(
            i => i.ProjectName === key
          );
          let ele = {
            InfectionProjectID:
              nitem !== undefined ? nitem.InfectionProjectID : 0,
            Name: item.value,
            ProjectName: item.key,
            CheckHospital: nitem !== undefined ? nitem.CheckHospital : "",
            CheckTime:
              nitem !== undefined
                ? nitem.CheckTime.split("T")[0]
                : `${curr.year}-${curr.month}-${curr.day}`
          };
          list.push(ele);
          temp.push(item.value);
        });
        this.infectionProjectValue = temp.toString();
        this.params.InfectionProject = list;
      }
    },
    focusCheckTime(event) {
      document.activeElement.blur();
    },
    btnSubmit(next) {
      this.indicator$.open("正在保存...");

      let params = {
        BaseInfoID: parseInt(this.params.BaseInfoID),
        UserID: this.user.UserID,
        Height: parseInt(this.params.Height),
        Weight: parseInt(this.params.Weight),
        Max_BP: parseInt(this.params.Max_BP),
        Min_BP: parseInt(this.params.Min_BP),
        AllergyHistory: this.params.AllergyHistory,
        InfectionProject: this.params.InfectionProject
      };

      if (params.BaseInfoID > 0) {
        MedicalRecordBaseInfoService.updateBaseInfo(params).then(response => {
          this.result(response, next);
        });
      } else {
        MedicalRecordBaseInfoService.addBaseInfo(params).then(response => {
          this.result(response, next);
        });
      }
    },
    result(response, next) {
      if (response && response.Data > 0) {
        this.toast$("保存成功！");
        if (next !== undefined) {
          let menu = JSON.parse(this.local$.getItem("tdMeLeftMenus"));
          menu.next = "next";
          this.$emit("change", menu);
        }
      } else {
        this.toast$("保存失败！");
      }
      this.getBaseInfo();
    }
  }
};
</script>
